"use strict";
/* Controllers */
app.controller("healthScoreTeamController", ['$scope', '$compile', '$location', '$rootScope', '$routeParams', 'healthScoreTeamService', function ($scope, $compile, $location, $rootScope, $routeParams, healthScoreTeamService) {

    var vm = $scope.vm;

    vm.p = {};
    vm.result = {};
    vm.p.group = 'owner_id'

    var errorFn = function (result) {
        alert(result);
    };

    var datePicker = $('.date');
    datePicker.datetimepicker({
        locale: 'zh-cn',
        format: 'YYYY-MM-DD'
    });
    datePicker.on('dp.hide', function (e) {
        var currentInput = $(e.currentTarget).find('input');
        var eName = currentInput.attr('name');
        var p = $scope.vm.p;
        p[eName] = currentInput.val();
        $scope.$apply()
    });

    if ($scope.vm.p.startDt == '' || typeof ($scope.vm.p.startDt) == 'undefined') {
        $scope.vm.p.startDt = new Date(new Date().getTime() - 2*86400000).format('yyyy-MM-dd');
    }

    if ($scope.vm.p.endDt == '' || typeof ($scope.vm.p.endDt) == 'undefined') {
        $scope.vm.p.endDt = new Date(new Date().getTime() - 2*86400000).format('yyyy-MM-dd');
    }

    $scope.queryReport = function () {
        $scope.listGroupHealthScore();
    };

    $scope.listGroupHealthScore = function () {
        $("#refreshTable").addClass('disabled');
        $("#list_table").bootstrapTable('showLoading')
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            $('#list_table').bootstrapTable('hideLoading');
            $("#refreshTable").removeClass('disabled');
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        healthScoreTeamService.listGroupHealthScore(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $scope.listGroupHealthScore();
    };

    $scope.initializeController();

}]);

/* Services */
app.service('healthScoreTeamService', ['ajaxService', function (ajaxService) {

    this.listGroupHealthScore = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/healthScore/listGroupHealthScore.do", successFunction, errorFunction);
    };

}]);

function TeamHcFmt(value, row, index) {

    //通过controller来获取Angular应用
    //获取$scope变量
    var optCont2 = ''
    optCont2 = '<a type="button" class="btn btn-xs btn-primary" href="#/star/cost/table_storage_health_score_list/all/' + row.ownerId + '" target="_blank">查看明细</a>'
    return [
        optCont2
    ].join('');
}

function filesizeFmt(value, row, index) {
    return internationalNumberShortFormatterStorage(value);
}

/**
 * 数据的国际化格式化方式，三个数据用逗号分隔 如123456转换成123,456
 */
function internationalNumberFormatter(value) {
    value += '';
    var x = value.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function internationalNumberShortFormatterStorage(value) {
    if (value == null || typeof value == 'undefined') {
        return "";
    }
    value += '';
    var x = value.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    var times = 0;
    while (rgx.test(x1)) {
        times++;
        //		x1 = x1.replace(rgx, '$1');
        x1 = changeTwoDecimalFx(x1 / 1024);
    }

    switch (times) {
        case 0:
            return internationalNumberFormatter(value) + "B";
        case 1:
            return x1 + "KB";
        case 2:
            return x1 + "MB";
        case 3:
            return x1 + "GB";
        case 4:
            return x1 + "TB";
        case 5:
            return x1 + "PB";
        case 6:
            return x1 + "EB";
        case 7:
            return x1 + "ZB";
        case 8:
            return x1 + "YB";
        default:
            return internationalNumberFormatter(value);
    }
}

function changeTwoDecimalFx(x) {
    var fx = parseFloat(x);
    if (isNaN(fx)) {
        alert('function:changeTwoDecimal->parameter error');
        return false;
    }
    fx = Math.round(fx * 100) / 100;
    var s_x = fx.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
    }
    return s_x;
}